using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EuropeEngulfed.NET
{
    partial class NavalCombatDialog : Form
    {
        private IGameStateInfoProvider gameStateProvider;
        private SeaMoveOperationalMove seaMoveToDoNavalCombatFor;
        private List<FleetPoint> friendlyFleetPoints;
        private List<FleetPoint> enemyFleetPoints;
        List<PlayingPiece> playingPiecesBeingTransported;
        private Bitmap targetImage;
        private bool reporting = false;

        private int numFriendlyHitsOn6 = 0;
        private int numFriendlyHitsOn56 = 0;
        private int numEnemyHitsOn6 = 0;
        private int numEnemyHitsOn56 = 0;

        public int NumFriendlyHitsOn6
        {
            get
            {
                return numFriendlyHitsOn6;
            }
        }

        public int NumFriendlyHitsOn56
        {
            get
            {
                return numFriendlyHitsOn56;
            }
        }

        public int NumEnemyHitsOn6
        {
            get
            {
                return numEnemyHitsOn6;
            }
        }

        public int NumEnemyHitsOn56
        {
            get
            {
                return numEnemyHitsOn56;
            }
        }

        private void ResolveNavalCombat(SeaMoveOperationalMove seaMoveToDoNavalCombatFor, Random r)
        {
            int numFriendlyFiringOn6 = 0;
            int numFriendlyFiringOn56 = 0;
            int numEnemyFiringOn6 = 0;       
            int numEnemyFiringOn56 = 0;

            if (enemyFleetPoints.Count > friendlyFleetPoints.Count)
            {
                numFriendlyFiringOn6 = friendlyFleetPoints.Count;
                numFriendlyFiringOn56 = 0;
                numEnemyFiringOn6 = friendlyFleetPoints.Count;
                numEnemyFiringOn56 = enemyFleetPoints.Count - friendlyFleetPoints.Count;
            }
            else if (friendlyFleetPoints.Count > enemyFleetPoints.Count)
            {
                numFriendlyFiringOn6 = enemyFleetPoints.Count;
                numFriendlyFiringOn56 = friendlyFleetPoints.Count - enemyFleetPoints.Count;
                numEnemyFiringOn6 = enemyFleetPoints.Count;
                numEnemyFiringOn56 = 0;
            }
            else
            {
                numFriendlyFiringOn6 = friendlyFleetPoints.Count;
                numFriendlyFiringOn56 = 0;
                numEnemyFiringOn6 = enemyFleetPoints.Count;
                numEnemyFiringOn56 = 0;
            }

            for(int i = 0; i < numFriendlyFiringOn6; i++)
            {
                uint dieRoll = (uint)(r.Next(6) + 1);
                if (dieRoll == 6)
                    numFriendlyHitsOn6++;
            }

            for (int i = 0; i < numFriendlyFiringOn56; i++)
            {
                uint dieRoll = (uint)(r.Next(6) + 1);
                if (dieRoll == 6 || dieRoll == 5)
                    numFriendlyHitsOn56++;
            }

            for (int i = 0; i < numEnemyFiringOn6; i++)
            {
                uint dieRoll = (uint)(r.Next(6) + 1);
                if (dieRoll == 6)
                    numEnemyHitsOn6++;
            }

            for (int i = 0; i < numEnemyFiringOn56; i++)
            {
                uint dieRoll = (uint)(r.Next(6) + 1);
                if (dieRoll == 6 || dieRoll == 5)
                    numEnemyHitsOn56++;
            }

            seaMoveToDoNavalCombatFor.FriendlyFleetLosses = numEnemyHitsOn6 + numEnemyHitsOn56;
            if (seaMoveToDoNavalCombatFor.FriendlyFleetLosses > friendlyFleetPoints.Count)
                seaMoveToDoNavalCombatFor.FriendlyFleetLosses = friendlyFleetPoints.Count;
            seaMoveToDoNavalCombatFor.EnemyFleetLosses = numFriendlyHitsOn6 + numFriendlyHitsOn56;
            if (seaMoveToDoNavalCombatFor.EnemyFleetLosses > enemyFleetPoints.Count)
                seaMoveToDoNavalCombatFor.EnemyFleetLosses = enemyFleetPoints.Count;
            if(seaMoveToDoNavalCombatFor.Player == GameController.playerType.AxisPlayer)
            {
                int numFrenchFleets = 0;
                foreach (FleetPoint enemyFleet in enemyFleetPoints)
                {
                    if (enemyFleet.CountryOwner == MapInfo.CountryID.France)
                        numFrenchFleets++;
                }

                seaMoveToDoNavalCombatFor.FrenchFleetLosses = Math.Min(numFrenchFleets, seaMoveToDoNavalCombatFor.EnemyFleetLosses);
            }
            else
            {
                int numFrenchFleets = 0;
                foreach (FleetPoint friendlyFleet in friendlyFleetPoints)
                {
                    if (friendlyFleet.CountryOwner == MapInfo.CountryID.France)
                        numFrenchFleets++;
                }

                seaMoveToDoNavalCombatFor.FrenchFleetLosses = Math.Min(numFrenchFleets, seaMoveToDoNavalCombatFor.FriendlyFleetLosses);
            }

            this.numFriendlyHitsOn6Label.Text = numFriendlyHitsOn6.ToString();
            this.numFriendlyHitsOn56Label.Text = numFriendlyHitsOn56.ToString();
            this.numEnemyHitsOn6Label.Text = numEnemyHitsOn6.ToString();
            this.numEnemyHitsOn56Label.Text = numEnemyHitsOn56.ToString();
        }

        public NavalCombatDialog(IGameStateInfoProvider gameStateProvider, GameController.playerType playerReportedTo, MapInfo.RegionID regionID, int numFriendlyHitsOn6, int numFriendlyHitsOn56, int numEnemyHitsOn6, int numEnemyHitsOn56, List<PlayingPiece> piecesTransported, List<PlayingPiece> piecesLost)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            InitializeComponent();
            file = thisExe.GetManifestResourceStream("EuropeEngulfed.NET.Resources.Target.bmp");
            targetImage = (Bitmap)Bitmap.FromStream(file);
            targetImage.MakeTransparent(System.Drawing.Color.White);
            file.Dispose();
            this.gameStateProvider = gameStateProvider;
            reporting = true;

            if(playerReportedTo == GameController.playerType.AxisPlayer)
            {
                if (regionID == MapInfo.RegionID.BalticSea)
                {
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, regionID);
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.USSR, regionID);
                }
                else if (regionID == MapInfo.RegionID.AtlanticOcean)
                {
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, regionID);
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, regionID);
                    friendlyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, regionID));
                }
                else if (regionID == MapInfo.RegionID.MediteraneanSea)
                {
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Italy, regionID);
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, regionID);
                    friendlyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, regionID));
                }
                else
                {
                    enemyFleetPoints = new List<FleetPoint>();
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.USSR, regionID);
                }
            }
            else
            {
                if (regionID == MapInfo.RegionID.BalticSea)
                {
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, regionID);
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.USSR, regionID);
                }
                else if (regionID == MapInfo.RegionID.AtlanticOcean)
                {
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, regionID);
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, regionID);
                    enemyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, regionID));
                }
                else if (regionID == MapInfo.RegionID.MediteraneanSea)
                {
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Italy, regionID);
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, regionID);
                    enemyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, regionID));
                }
            }

            this.navalCombatDialogTitleLabel.Text = "Naval Combat In " + Enum.GetName(typeof(MapInfo.RegionID), regionID);
            this.enemyFleetListView.View = View.Tile;
            this.enemyFleetListView.TileSize = new Size(PlayingPieceGraphics.GetFleetSize().Width * 2, PlayingPieceGraphics.GetFleetSize().Height * 2);
            this.friendlyFleetListView.View = View.Tile;
            this.friendlyFleetListView.TileSize = new Size(PlayingPieceGraphics.GetFleetSize().Width * 2, PlayingPieceGraphics.GetFleetSize().Height * 2);
            this.piecesLostListView.View = View.Tile;
            this.piecesLostListView.TileSize = new Size(PlayingPieceGraphics.GetBlockSize().Width * 2, PlayingPieceGraphics.GetBlockSize().Height * 2);

            foreach (FleetPoint enemyFleet in enemyFleetPoints)
            {
                this.enemyFleetListView.Items.Add(enemyFleet.GetDescription());
            }
            foreach (FleetPoint friendlyFleet in friendlyFleetPoints)
            {
                this.friendlyFleetListView.Items.Add(friendlyFleet.GetDescription());
            }

            if(piecesTransported.Count > 0)
            {
                playingPiecesBeingTransported = piecesTransported;
                this.piecesLostListView.Visible = true;
                this.Size = new Size(this.Width, 690);
                this.okButton.Location = new Point(this.okButton.Location.X, 510);
                List<PlayingPiece> copyOfPlayingPieceLost = new List<PlayingPiece>(piecesLost);
                foreach (PlayingPiece pieceBeingTransported in piecesTransported)
                {
                    this.piecesLostListView.Items.Add(pieceBeingTransported.GetDescription());
                    foreach (PlayingPiece pieceLost in copyOfPlayingPieceLost)
                    {
                        if (pieceLost.Equals(pieceBeingTransported))
                        {
                            this.piecesLostListView.Items[this.piecesLostListView.Items.Count - 1].Selected = true;
                            break;
                        }
                    }
                }
            }

            if (enemyFleetPoints.Count > friendlyFleetPoints.Count)
            {
                this.numFriendlyShipsFiringOn6Label.Text = friendlyFleetPoints.Count.ToString();
                this.numFriendlyShipsFiringOn56Label.Text = "0";
                this.numEnemyShipsFiringOn6Label.Text = friendlyFleetPoints.Count.ToString();
                this.numEnemyShipsFiringOn56Label.Text = "" + (enemyFleetPoints.Count - friendlyFleetPoints.Count);
            }
            else if (friendlyFleetPoints.Count > enemyFleetPoints.Count)
            {
                this.numFriendlyShipsFiringOn6Label.Text = enemyFleetPoints.Count.ToString();
                this.numFriendlyShipsFiringOn56Label.Text = "" + (friendlyFleetPoints.Count - enemyFleetPoints.Count);
                this.numEnemyShipsFiringOn6Label.Text = enemyFleetPoints.Count.ToString();
                this.numEnemyShipsFiringOn56Label.Text = "0";
            }
            else
            {
                this.numFriendlyShipsFiringOn6Label.Text = friendlyFleetPoints.Count.ToString();
                this.numFriendlyShipsFiringOn56Label.Text = "0";
                this.numEnemyShipsFiringOn6Label.Text = enemyFleetPoints.Count.ToString(); ;
                this.numEnemyShipsFiringOn56Label.Text = "0";
            }

            this.numFriendlyHitsOn6Label.Text = numFriendlyHitsOn6.ToString();
            this.numFriendlyHitsOn56Label.Text = numFriendlyHitsOn56.ToString();
            this.numEnemyHitsOn6Label.Text = numEnemyHitsOn6.ToString();
            this.numEnemyHitsOn56Label.Text = numEnemyHitsOn56.ToString();
        }

        public NavalCombatDialog(IGameStateInfoProvider gameStateProvider, SeaMoveOperationalMove seaMoveToDoNavalCombatFor, Random random)
        {
            System.Reflection.Assembly thisExe;
            thisExe = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            InitializeComponent();
            file = thisExe.GetManifestResourceStream("EuropeEngulfed.NET.Resources.Target.bmp");
            targetImage = (Bitmap)Bitmap.FromStream(file);
            targetImage.MakeTransparent(System.Drawing.Color.White);
            file.Dispose();
            this.seaMoveToDoNavalCombatFor = seaMoveToDoNavalCombatFor;
            this.gameStateProvider = gameStateProvider;
            reporting = false;
            if (seaMoveToDoNavalCombatFor.Player == GameController.playerType.AxisPlayer)
            {
                if (seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough == MapInfo.RegionID.BalticSea)
                {
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.USSR, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                }
                else if (seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough == MapInfo.RegionID.AtlanticOcean)
                {
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    enemyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough));
                }
                else if (seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough == MapInfo.RegionID.MediteraneanSea)
                {
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Italy, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    enemyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough));
                }
            }
            else
            {
                if (seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough == MapInfo.RegionID.BalticSea)
                {
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.USSR, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                }
                else if (seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough == MapInfo.RegionID.AtlanticOcean)
                {
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Germany, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    friendlyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough));
                }
                else if (seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough == MapInfo.RegionID.MediteraneanSea)
                {
                    enemyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.Italy, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.GreatBritain, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                    friendlyFleetPoints.AddRange(gameStateProvider.GetFleetPointList(MapInfo.CountryID.France, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough));
                }
                else
                {
                    enemyFleetPoints = new List<FleetPoint>();
                    friendlyFleetPoints = gameStateProvider.GetFleetPointList(MapInfo.CountryID.USSR, seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
                }
            }

            this.navalCombatDialogTitleLabel.Text = "Naval Combat In " + Enum.GetName(typeof(MapInfo.RegionID), seaMoveToDoNavalCombatFor.SeaRegionTransportedThrough);
            this.enemyFleetListView.View = View.Tile;
            this.enemyFleetListView.TileSize = new Size(PlayingPieceGraphics.GetFleetSize().Width * 2, PlayingPieceGraphics.GetFleetSize().Height * 2);
            this.friendlyFleetListView.View = View.Tile;
            this.friendlyFleetListView.TileSize = new Size(PlayingPieceGraphics.GetFleetSize().Width * 2, PlayingPieceGraphics.GetFleetSize().Height * 2);
            this.piecesLostListView.View = View.Tile;
            this.piecesLostListView.TileSize = new Size(PlayingPieceGraphics.GetBlockSize().Width * 2, PlayingPieceGraphics.GetBlockSize().Height * 2);

            foreach (FleetPoint enemyFleet in enemyFleetPoints)
            {
                this.enemyFleetListView.Items.Add(enemyFleet.GetDescription());
            }
            foreach (FleetPoint friendlyFleet in friendlyFleetPoints)
            {
                this.friendlyFleetListView.Items.Add(friendlyFleet.GetDescription());
            }
            playingPiecesBeingTransported = seaMoveToDoNavalCombatFor.GetListOfPiecesBeingTransported();
            foreach(PlayingPiece pieceBeingTransported in playingPiecesBeingTransported)
            {
                this.piecesLostListView.Items.Add(pieceBeingTransported.GetDescription());
            }

            if (enemyFleetPoints.Count > friendlyFleetPoints.Count)
            {
                this.numFriendlyShipsFiringOn6Label.Text = friendlyFleetPoints.Count.ToString();
                this.numFriendlyShipsFiringOn56Label.Text = "0";
                this.numEnemyShipsFiringOn6Label.Text = friendlyFleetPoints.Count.ToString();
                this.numEnemyShipsFiringOn56Label.Text = "" + (enemyFleetPoints.Count - friendlyFleetPoints.Count);
            }
            else if (friendlyFleetPoints.Count > enemyFleetPoints.Count)
            {
                this.numFriendlyShipsFiringOn6Label.Text = enemyFleetPoints.Count.ToString();
                this.numFriendlyShipsFiringOn56Label.Text = "" + (friendlyFleetPoints.Count - enemyFleetPoints.Count);
                this.numEnemyShipsFiringOn6Label.Text = enemyFleetPoints.Count.ToString();
                this.numEnemyShipsFiringOn56Label.Text = "0";
            }
            else
            {
                this.numFriendlyShipsFiringOn6Label.Text = friendlyFleetPoints.Count.ToString();
                this.numFriendlyShipsFiringOn56Label.Text = "0";
                this.numEnemyShipsFiringOn6Label.Text = enemyFleetPoints.Count.ToString(); ;
                this.numEnemyShipsFiringOn56Label.Text = "0";
            }

            ResolveNavalCombat(seaMoveToDoNavalCombatFor, random);
        }

        private void friendlyFleetListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawText(TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter);
            PlayingPieceGraphics.PaintPlayingPiece(e.Graphics, friendlyFleetPoints[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            e.DrawFocusRectangle();
        }

        private void EnemyFleetListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawText(TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter);
            PlayingPieceGraphics.PaintPlayingPiece(e.Graphics, enemyFleetPoints[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            e.DrawFocusRectangle();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if(!reporting)
            {
                if (!this.piecesLostListView.Visible)
                {
                    if ((friendlyFleetPoints.Count - seaMoveToDoNavalCombatFor.FriendlyFleetLosses) < playingPiecesBeingTransported.Count)
                    {
                        this.piecesLostListView.Visible = true;
                        this.Size = new Size(this.Width, 690);
                        this.okButton.Location = new Point(this.okButton.Location.X, 510);
                        this.piecesToLoseLabel.Location = new Point(this.piecesToLoseLabel.Location.X, 515);
                        this.piecesToLoseLabel.Text = "Select " + (playingPiecesBeingTransported.Count - (friendlyFleetPoints.Count - seaMoveToDoNavalCombatFor.FriendlyFleetLosses)) + " units to lose";
                        this.piecesToLoseLabel.Visible = true;
                        this.infoLabel.Location = new Point(this.piecesToLoseLabel.Location.X, 500);
                        this.infoLabel.Visible = true;
                    }
                    else
                        this.Close();
                }
                else
                {
                    if (this.piecesLostListView.SelectedItems.Count != (playingPiecesBeingTransported.Count - (friendlyFleetPoints.Count - seaMoveToDoNavalCombatFor.FriendlyFleetLosses)))
                        MessageBox.Show("You must select " + (playingPiecesBeingTransported.Count - (friendlyFleetPoints.Count - seaMoveToDoNavalCombatFor.FriendlyFleetLosses)) + " units to lose");
                    else
                    {
                        foreach (ListViewItem listViewItem in piecesLostListView.SelectedItems)
                        {
                            seaMoveToDoNavalCombatFor.AddPieceLost(playingPiecesBeingTransported[listViewItem.Index]);
                        }
                        this.Close();
                    }
                }
            }
            else
            {
                this.Close();
            }
        }

        private void piecesLostListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            e.DrawText(TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter);
            if(!reporting)
                PlayingPieceGraphics.PaintPlayingPiece(e.Graphics, playingPiecesBeingTransported[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            else
            {
                if(playingPiecesBeingTransported[e.ItemIndex] is Block)
                {
                    PlayingPieceGraphics.PaintBlock(e.Graphics, (Block)playingPiecesBeingTransported[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)), 1, true);
                }
                else
                    PlayingPieceGraphics.PaintPlayingPiece(e.Graphics, playingPiecesBeingTransported[e.ItemIndex], new Point(e.Bounds.Left + (e.Bounds.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2)));
            }
            
            if (e.Item.Selected)
            {
                e.Graphics.DrawImage(targetImage, new Point(e.Bounds.X + e.Bounds.Width / 2 - targetImage.Width / 2, e.Bounds.Y + e.Bounds.Height / 2 - targetImage.Height / 2));
            }
        }
    }
}